package zhihui.mobile.ui.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.MyEditText;
import com.zgm.utils.ui.UiHeaderLayout;

import cn.jpush.android.api.JPushInterface;
import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.constants.ActivitySourceTag;
import zhihui.mobile.listener.EdDrawablePasswordListener;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.login.ILoginContract;
import zhihui.mobile.presenter.login.LoginPresenter;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.register.RegisterActivity;
import zhihui.mobile.ui.register.RetrievePasswordActivity;

/**
 * Created by 张公铭 on 2017/6/28 0028.
 */
public class LoginActivity extends BaseActivity implements ILoginContract.ILogionView
{
    ILoginContract.ILoginPresenter loginPresenter = new LoginPresenter();
    private UiHeaderLayout mHeader;
    private EditText ed_userName;
    private MyEditText ed_password;
    private TextView tv_register;
    private TextView tv_find_password;
    private TextView tv_msg;
    private Button btn_logion;
    private String tag = "none";

    @Override
    protected int getLayoutResId()
    {
        return R.layout.login_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{loginPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        loginPresenter.init(this, this);
    }

    @Override
    public void initView()
    {
        mHeader = (UiHeaderLayout) findViewById(R.id.header_login);
        mHeader.setTitle("用户登录");
        mHeader.hindRightOneButton();
        mHeader.hindRightTwoButton();
        mHeader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (TextUtils.isEmpty(tag) || tag.equals("none"))
                {
                    goBack(LoginActivity.class);
                } else if(tag.equals(ActivitySourceTag.ShopDetailsActivity))
                {
                    loginPresenter.goShopDetailActivity();
                    MyApplication.finishActivity(LoginActivity.class);
                }
            }
        });

        ed_userName = (EditText) findViewById(R.id.ed_login_username);
        UiUtils.editTextFocus(ed_userName);

        ed_password = (MyEditText) findViewById(R.id.ed_login_password);
        ed_password.setDrawableRightListener(new EdDrawablePasswordListener(this));

        tv_register = (TextView) findViewById(R.id.tv_login_register);
        tv_register.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goRegisterActivity();
            }
        });

        tv_find_password = (TextView) findViewById(R.id.tv_login_find_password);
        tv_find_password.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goFindPassword();
            }
        });

        tv_msg = (TextView) findViewById(R.id.tv_login_msg);
        tv_msg.setText("");
        btn_logion = (Button) findViewById(R.id.btn_login_login);
        btn_logion.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                tv_msg.setText(" ");
                loginPresenter.login(ed_userName.getText().toString(), ed_password.getText().toString(), "");
            }
        });

        loginPresenter.initUserInfo();
    }

    @Override
    protected void parseArgumentsFormIntent(Intent argIntent)
    {
        tag = argIntent.getStringExtra("tag");
    }

    private void goFindPassword()
    {
        Intent intent = new Intent(this, RetrievePasswordActivity.class);
        startActivity(intent);
    }

    /**
     * 注册页
     */
    private void goRegisterActivity()
    {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    public void showDialog(String title, String message)
    {
        UiUtils.showDialog(this, title, message);
    }

    @Override
    public void initData(String userName, String password)
    {
        ed_userName.setText(userName);
        ed_password.setText(password);
        UiUtils.editTextFocus(ed_password, password.length());
    }

    @Override
    public void hindMessage()
    {
        tv_msg.setText("");
    }

    @Override
    public void goBack(Class cls)
    {
        if (TextUtils.isEmpty(tag) || tag.equals("none"))
        {
            super.goBack(LoginActivity.class);
        } else if(tag.equals(ActivitySourceTag.ShopDetailsActivity))
        {
            loginPresenter.goShopDetailActivity();
        }
    }

    @Override
    public void showMessage(String message)
    {
        tv_msg.setText(message);
        tv_msg.setVisibility(View.VISIBLE);
    }
}
